package com.bwda.dsrs.filecenter.util;

import java.awt.*;

/**
 * 颜色处理工具类
 *
 * @author youzhen
 * @date 2019/4/11
 */
public final class ColorUtil {

    /**
     * colorStr 转 Color三色素
     */
    public final static Integer[] getColorList(String colorStr) {
        String[] color = colorStr.split(",");
        Integer[] colorList = {Integer.parseInt(color[0]),Integer.parseInt(color[1]),Integer.parseInt(color[2])};
        return colorList;
    }


    /**
     * colorStr 转 Color
     */
    public final static Color getColor(String colorStr) {
        colorStr=hex2Rgb(colorStr);
        String[] strarr = colorStr.split(",");
        Color color = new Color(Integer.parseInt(strarr[0]),Integer.parseInt(strarr[1]),Integer.parseInt(strarr[2]));
        return color;
    }

    public static String hex2Rgb(String hex) {
        StringBuilder sb = new StringBuilder();
        String c = hex.toUpperCase().replace("#", "");
        String r = Integer.parseInt((c.length() == 3 ? c.substring(0, 1) + c.substring(0, 1) : c.substring(0, 2)), 16) + "";
        String g = Integer.parseInt((c.length() == 3 ? c.substring(1, 2) + c.substring(1, 2) : c.substring(2, 4)), 16) + "";
        String b = Integer.parseInt((c.length() == 3 ? c.substring(2, 3) + c.substring(2, 3) : c.substring(4, 6)), 16) + "";
        sb.append(r).append(",").append(g).append(",").append(b);
        return sb.toString();
    }

}
